<!--
 * 菜单管理
 * @author: hexiaozhen
 * @since: 2023-09-27
 * index.vue
-->
<template>
  <div class="main-content">
    <!-- 搜索 -->
    <TableForm
      ref="formAndTableRef"
      :formItems="formItems"
      :formData="formData"
      :girdOption="girdOption"
      class="tableForm"
      @getTableList="getTableList"
      @handleBtnClick="handleBtnClick"
    >
      <!-- 自定义操作按钮 -->
      <template #tableOptate="{ record }">
        <a-button
          v-for="clickItem in tableOperateList"
          :key="clickItem.text"
          type="text"
          class="operate-item"
          :status="clickItem.eventName == 'tableDel' ? 'danger' : ''"
          @click="handleBtnClick(clickItem.eventName, record)"
        >
          {{ clickItem.text }}
        </a-button>
      </template>
      <!-- 列插槽 -->
      <template #createTime="{ record }">
        {{ record.createTime.slice(0, 10) }}
      </template>
      <template #status="{ record }">
        {{ record.status === '1' ? '停用' : '开启' }}
      </template>
    </TableForm>
    <AddEditMenu v-if="visible" v-model:visible="visible" :rowData="rowData" @submit="getTableList()"></AddEditMenu>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import TableForm from '@/components/tableForm/index.vue'
import AddEditMenu from './components/addEditMenu.vue' // 新增编辑
import system from '@/api/modules/system'
import { Modal, Message } from '@arco-design/web-vue'
// import { useRouter } from 'vue-router'
// const router = useRouter()

const formData = reactive({ menuName: '', status: '' })
const formAndTableRef = ref()
// 获取字典数据
const statusOptions = ref([])
// 表单配置
const formItems = reactive([
  { name: 'menuName', label: '菜单名称', type: 'a-input' },
  { name: 'status', label: '状态', type: 'a-select', options: statusOptions },
])
const girdOption = reactive({
  btnList: [{ name: '新增', eventName: 'add', type: 'icon-xinzeng' }],
  columns: [
    { title: '菜单名称', dataIndex: 'menuName', width: 180 },
    { title: '排序', dataIndex: 'orderNum', width: 80 },
    { title: '权限标识', dataIndex: 'perms', width: 150 },
    { title: '组件路径', dataIndex: 'component', width: 150 },
    { title: '状态', slotName: 'status', width: 68 },
    { title: '创建时间', slotName: 'createTime', width: 125 },
    { title: '操作', slotName: 'optional', width: 160, fixed: 'right' },
  ],
  loading: false,
  pagination: false, // 无分页
  tableId: 'menuId', //表格唯一标识符类似于id
  tableSlotList: ['createTime', 'status'],
})

// 表格操作插槽
const tableOperateList = reactive([
  { text: '修改', eventName: 'tableEdit' },
  { text: '新增', eventName: 'tableAdd' },
  { text: '删除', eventName: 'tableDel' },
])
let visible = ref(false) // 新增编辑弹窗
let rowData = reactive({ type: 'add' }) // 弹窗数据

const handleBtnClick = (type, row) => {
  switch (type) {
    case 'add': {
      // 新增
      rowData = { type: 'add' }
      visible.value = true
      // router.push({ path: '/system/menu/addEditMenu', query: { type: 'add' } })
      break
    }
    case 'tableAdd': {
      // 表格新增
      rowData = { ...row, type: 'add' }
      visible.value = true
      break
    }
    case 'tableEdit': {
      //编辑
      visible.value = true
      rowData = { ...row, type: 'edit' }
      break
    }
    case 'tableDel': {
      //删除
      Modal.open({
        title: '警告',
        content: `是否确认删除菜单名称为${row.menuName}的数据项？`,
        draggable: true,
        onOk: () => {
          delMenu(row.menuId)
        },
      })
      break
    }
  }
}
const delMenu = (id) => {
  system.delMenu(id).then((res) => {
    getTableList()
    Message.success(res.msg)
  })
}
// 获取表格数据
const getTableList = () => {
  girdOption.loading = true
  system
    .getMenuList({ ...formData })
    .then((res) => {
      formAndTableRef.value.setTableData(res.data)
      girdOption.loading = false
    })
    .catch(() => {
      girdOption.loading = false
    })
}
// 数据字典
const getOption = () => {
  const param = { dictTypes: 'sys_normal_disable' }
  system.getDicts(param).then((res) => {
    statusOptions.value = res.data.sys_normal_disable.map((it) => ({
      label: it.dictLabel,
      value: it.dictValue,
    }))
  })
}
onMounted(() => {
  getOption()
  getTableList()
})
</script>
